﻿using PokerGame;
using System;
using UnityEngine.SceneManagement;

namespace Framework
{
    public class SceneMgr : ManagerBase
    {
        public static SceneMgr Instance;

        private Action OnSceneLoaded = null;

        private void Awake()
        {
            Instance = this;
            Add(SceneEvent.LOAD_SCENE, this);
            SceneManager.sceneLoaded += SceneManager_sceneLoaded;
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case SceneEvent.LOAD_SCENE:
                    LoadScene(message as LoadSceneMsg);
                    break;
                default:
                    break;
            }
        }
        private void LoadScene(LoadSceneMsg msg)
        {
            if (msg.SceneBuildIndex != -1)

                SceneManager.LoadScene(msg.SceneBuildIndex);

            if (msg.SceneBuildName != null)

                SceneManager.LoadScene(msg.SceneBuildName);
            if (msg.OnSceneLoaded != null)
                OnSceneLoaded = msg.OnSceneLoaded;

        }

        private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode mode)
        {
            OnSceneLoaded?.Invoke();
        }
    }
}
